﻿using MMO_MANAGER.DAO;
using MMO_MANAGER.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
namespace MMO_MANAGER.BUS
{
    class channel_list_bus
    {
        private AccessData dao;
        private SQLiteDataReader dr;
        public channel_list_bus()
        {
            dao = new AccessData();
        }

        public DataTable GetData()
        {
            string sql = "select * from channel_list where active=@act ORDER BY id ASC";
            var param = new SQLiteParameter[1];
            param[0] = new SQLiteParameter("@act", DbType.Int16);
            param[0].Value = 1;

            DataTable dt = dao.GetDataSet(sql, param, false).Tables[0];
            return dt;
        }

        public DataTable GetListChannelByName(channel_list cl)
        {
            string sql = "select * from channel_list where ten=@ten and active=@act";
            var param = new SQLiteParameter[2];
            param[0] = new SQLiteParameter("@act", DbType.Int16);
            param[0].Value = 1;
            param[1] = new SQLiteParameter("@ten", DbType.String);
            param[1].Value = cl.ten;

            DataTable dt = dao.GetDataSet(sql, param, false).Tables[0];
            return dt;
        }

        // - Add : 
        public void Insert(channel_list cl)
        {
            try
            {
                string sql = "Insert Into channel_list (id_email, id_gpage, ten, id_label, id_ga, ghichu, active, ngayedit) " +
                "values (@ide, @idg, @ten, @idl, @idga, @ghichu, @act, @ngayedit)";
            var param = new SQLiteParameter[8];
            param[0] = new SQLiteParameter("@ide", DbType.Int16);
            param[0].Value = cl.id_email;
            param[1] = new SQLiteParameter("@idg", DbType.Int16);
            param[1].Value = cl.id_gpage;
            param[2] = new SQLiteParameter("@ten", DbType.String);
            param[2].Value = cl.ten;
            param[3] = new SQLiteParameter("@idl", DbType.Int16);
            param[3].Value = cl.id_label;
            param[4] = new SQLiteParameter("@idga", DbType.Int16);
            param[4].Value = cl.id_ga;
            param[5] = new SQLiteParameter("@ghichu", DbType.String);
            param[5].Value = cl.ghichu;
            param[6] = new SQLiteParameter("@act", DbType.Int16);
            param[6].Value = cl.active;
            param[7] = new SQLiteParameter("@ngayedit", DbType.DateTime);
            param[7].Value = cl.ngayedit;
            dao.ExecuteNoneQuery(sql, param);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        // - Edit: not edit 'active'
        public void Update(channel_list cl)
        {
            try
            {
            string sql = "Update channel_list SET id_email=@ide, id_gpage=@idg, ten=@ten, id_label=@idl, id_ga=@idga, ghichu=@ghichu, ngayedit=@ngayedit where id=@id";
            var param = new SQLiteParameter[8];
            param[0] = new SQLiteParameter("@ide", DbType.Int16);
            param[0].Value = cl.id_email;
            param[1] = new SQLiteParameter("@idg", DbType.Int16);
            param[1].Value = cl.id_gpage;
            param[2] = new SQLiteParameter("@ten", DbType.String);
            param[2].Value = cl.ten;
            param[3] = new SQLiteParameter("@idl", DbType.Int16);
            param[3].Value = cl.id_label;
            param[4] = new SQLiteParameter("@idga", DbType.Int16);
            param[4].Value = cl.id_ga;
            param[5] = new SQLiteParameter("@ghichu", DbType.String);
            param[5].Value = cl.ghichu;
            param[6] = new SQLiteParameter("@id", DbType.Int16);
            param[6].Value = cl.id;
            param[7] = new SQLiteParameter("@ngayedit", DbType.DateTime);
            param[7].Value = cl.ngayedit;
            dao.ExecuteNoneQuery(sql, param);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        // - Delete: update 'active' = 0
        public void Delete(channel_list cl)
        {
            string sql = "Update channel_list SET active=@act where id=@id";
            var param = new SQLiteParameter[2];
            param[0] = new SQLiteParameter("@id", DbType.Int16);
            param[0].Value = cl.id;
            param[1] = new SQLiteParameter("@act", DbType.Int16);
            param[1].Value = cl.active;
            dao.ExecuteNoneQuery(sql, param);
        }

    }
}
